<?php
/**
 * User: b179a9
 * Date: 10/11/11
 * Time: 9:00 PM
 */

class PhotoController extends Zend_Controller_Action
{

    /**
     * FlashMessenger
     *
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('admin-layout');
        }
        $this->_flashMessenger = new Lib_Flash();
    }

    public function __call($name, $args)
    {
        $this->_flashMessenger->addMessage('Action does not exist.', Lib_Flash::ERROR, Lib_Flash::PRE);
        $this->_redirect('/admin');
    }

    public function addAction()
    {
        $answer = $this->_request->getParam('answer');
        $answersManager = new \Managers\Answers();
        
        if ($this->_request->isPost()) {
            $file = $_FILES['img'];
            try {
                $answersManager->addPhoto($answer, $file);
                $this->_flashMessenger->addMessage('Photo successfully added.', Lib_Flash::NOTICE, Lib_Flash::POST);
            } catch (ImagickException $e) {
                $this->_flashMessenger->addMessage('This file is not supported.', Lib_Flash::ERROR, Lib_Flash::POST);
            } catch (Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::POST);
            }
        }
        $ans = $answersManager->getById($answer);
        $this->view->answer = $ans;
    }

}
